import wx_opendata from "./wx/wx_opendata";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

declare const sharedCanvas;
declare const pomelo;

@ccclass
export default class NewClass extends cc.Component {

  

    @property(cc.Label)
    friendLabel: cc.Label = null;


    @property(cc.Label)
    worldLabel: cc.Label = null;

    @property(cc.Sprite)
    rankingView: cc.Sprite = null;

    worldRankingDatas=[];
    worldRankingPage=0;
    worldRankingTotalPage=0;

  

    tex;
  
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        if(window["sharedCanvas"]){
            this.tex = new cc.Texture2D();
            sharedCanvas.width = this.rankingView.node.width;
            sharedCanvas.height = this.rankingView.node.height;
        }
    }

    start () {
        this.friendLabel.node.color=cc.Color.WHITE;
        this.worldLabel.node.color=cc.Color.GRAY;
    }

 
     updateSubDomainCanvas() {
        this.tex.initWithElement(window["sharedCanvas"]);
        this.tex.handleLoadedTexture();
        this.rankingView.spriteFrame = new cc.SpriteFrame(this.tex);
    }
    update() {
         if(window["wx"]){
              this.updateSubDomainCanvas();
         }
    }

    onClose(){
        this.node.parent.active=false;
    }

    onFriendRankingClick(e){
        this.friendLabel.node.color=cc.Color.WHITE;
        this.worldLabel.node.color=cc.Color.GRAY;

        if(window["wx"]){
            wx_opendata.wxPostMessageToSubDomain({
                action:wx_opendata.WxDomainAction.FetchFriend,
                data:{
                    page:0
                }
            });
        }
    }

    onWorldRankingClick(e){
        this.friendLabel.node.color=cc.Color.GRAY;
        this.worldLabel.node.color=cc.Color.WHITE;

        wx_opendata.ranking.setLoading(wx_opendata.wxGetSharedCanvas().getContext('2d'));

        pomelo.request('game.game_handler.getWorldRanking',{},(res)=>{
            console.log(res);

            if(res.code!==200){
                wx_opendata.ranking.setError(wx_opendata.wxGetSharedCanvas().getContext('2d'));
                return;
            }

            this.worldRankingDatas=res.data;
            const dataLen = res.data.length;
            this.worldRankingPage = 0;
            this.worldRankingTotalPage =wx_opendata.ranking.getTotalPage(dataLen);
            if(dataLen)
            {
               wx_opendata.ranking.showPagedRanks(wx_opendata.wxGetSharedCanvas().getContext('2d'),0,this.worldRankingDatas);
            }
            else{
                wx_opendata.ranking.setEmpty(wx_opendata.wxGetSharedCanvas().getContext('2d'));
            }


        });

    
      
    }

    
}
